﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.MySQL;
using System.IO;
using INHSStudentRegistration;

namespace IrisanNHS
{
    public partial class Form7 : Form
    {
        MySQL_Database MyDB = new MySQL_Database();

        public Form7()
        {
            InitializeComponent();
            btnLogin.Click += new EventHandler(btnLogin_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            this.Load += new EventHandler(Form7_Load);
        }

        void Form7_Load(object sender, EventArgs e)
        {
            string[] dt = File.ReadAllLines(@".\\config.txt");
            txtserver.Text = dt[0];
            txtdatabase.Text = dt[1];
            txtuserSettings.Text = dt[2];
            txtPassSettings.Text = dt[3];
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter(@".\\config.txt");
            sw.WriteLine("Settings Configuration");
            sw.WriteLine(txtserver.Text);
            sw.WriteLine(txtdatabase.Text);
            sw.WriteLine(txtuserSettings.Text);
            sw.WriteLine(txtPassSettings.Text);
            sw.Close();
            MessageBox.Show("Successfully save");
        }

        void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtuser.Text != "" || txtpass.Text != "")
            {
                MyDB.Connection_Database("SELECT * FROM tblusers WHERE Username = '" + txtuser.Text + "' AND Password = '" + txtpass.Text + "'");
                if (MyDB.dt.Rows.Count > 0)
                {
                    string access = MyDB.dt.Rows[0]["Level"].ToString();
                    //MainForm frm = new MainForm(txtpass.Text);
                    MainForm frm = new MainForm(txtpass.Text);
                    //frm.level(access);
                    
                    this.Hide();
                    //frm.ShowDialog();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Incorrect Password or Username");
                }
            }
        }
    }
}
